package com.dcivision.framework.taglib.channel;

import java.util.Stack;

import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.dcivision.framework.SystemParameterConstant;
import com.dcivision.framework.SystemParameterFactory;

/**
 * AjaxTag pool 
 * @author tc tang
 *
 */

public class AjaxTagContainer {

  private static final Log log = LogFactory.getLog(AjaxTagContainer.class);
  
  private final static int count = Integer.parseInt(SystemParameterFactory.getSystemParameter(SystemParameterConstant.CHANNEL_TAG_POOL_SIZE));//default;
  
  private String tagName = null ;
  
  private Stack pool = new Stack();
  
  public AjaxTagContainer(String tagName){
    this.tagName = tagName;
  }
  
  public TagSupport getTag(){
    
      try {
        if(pool.empty()){
          
          return (TagSupport) Class.forName(tagName).newInstance();
          
        }
      } catch (InstantiationException exp) {
        log.error("Instant class "+tagName+" faild!",exp);
      } catch (IllegalAccessException exp) {
        log.error("Access class "+tagName+" faild!",exp);
      } catch (ClassNotFoundException exp) {
        log.error("Not found class "+tagName+"!",exp);
      }
   
    return (TagSupport) pool.pop();
  
  }
  
  public void putTag(TagSupport tag){
    if(pool.size()>=count){
      //pool is overflow
    }else{
      tag.release();//release the tag
      pool.push(tag);
    }
  }
  
}
